﻿<Serializable()>
Public Class Map

    Private _Title As String
    Private _Number As Integer
    Private _Revision As Integer
    Private _Tiles(,) As Tile

    Public Sub New()

        ReDim _Tiles(Settings.MaximumMapX, Settings.MaximumMapY)

        For x As Integer = 0 To Settings.MaximumMapX
            For y As Integer = 0 To Settings.MaximumMapY
                _Tiles(x, y) = New Tile
            Next
        Next

    End Sub

    Public Property Number As Integer
        Get
            Return _Number
        End Get
        Set(ByVal value As Integer)
            _Number = value
        End Set
    End Property

    Public Property Title As String
        Get
            Return _Title
        End Get
        Set(ByVal value As String)
            _Title = value
        End Set
    End Property

    Public Property Revision As Integer
        Get
            Return _Revision
        End Get
        Set(ByVal value As Integer)
            _Revision = value
        End Set
    End Property

    Public Property Tiles(ByVal x As Integer, ByVal y As Integer) As Tile
        Get
            Return _Tiles(x, y)
        End Get
        Set(ByVal value As Tile)
            _Tiles(x, y) = value
        End Set
    End Property

    Public Sub LoadMap()

        If File.Exists(MapsPath & _Number & Settings.MapsFormat) Then

            Dim StreamFile As FileStream = File.OpenRead(MapsPath & _Number & Settings.MapsFormat)
            Dim Deserializer As New BinaryFormatter()
            Maps(_Number) = CType(Deserializer.Deserialize(StreamFile), Map)
            StreamFile.Close()

        Else

            WriteMap()
            LoadMap()

        End If

    End Sub

    Public Sub WriteMap()

        Dim StreamFile As FileStream = File.Create(MapsPath & _Number & Settings.MapsFormat)
        Dim Serializer As New BinaryFormatter()
        Serializer.Serialize(StreamFile, Me)
        StreamFile.Close()

    End Sub

    Public Shared Sub LoadAllMaps()
        For i As Integer = 1 To Settings.MaximumMaps
            Maps(i).LoadMap()
        Next
    End Sub

    Public Shared Sub WriteAllMaps()
        For i As Integer = 1 To Settings.MaximumMaps
            Maps(i).WriteMap()
        Next
    End Sub

End Class
